package com.h3c.ptability.controller;

import com.eos.common.constant.StatusCodeEnum;
import com.eos.common.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h3c.ptability.async.AsyncSendMessage;
import com.h3c.ptability.entity.PtTodoTask;
import com.h3c.ptability.service.IPtTodoTaskService;
import com.h3c.ptability.dto.PtTodoTaskDTO;
import com.h3c.ptability.service.IPtTodoTaskService;
import com.h3c.ptability.utils.ReturnCodeData;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;

import javax.annotation.Resource;
import java.util.List;

/**
 * Copyright (C), 2022-2022, h3c
 * FileName: PtTodoTaskController
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_todo_task controller
 * Version: 1.0
 */
@Api(tags = " PtTodoTask 接口")
@CrossOrigin
@Slf4j
@RestController
@RequestMapping(value = "/ptTodoTask",produces = "application/json;charset=utf-8")
public class PtTodoTaskController {

    @Resource
    private IPtTodoTaskService ptTodoTaskService;

    @Resource
    private AsyncSendMessage asyncSendMessage;


    /**
     * @title: queryFuzzyListForPage
     * @description: 列表分页模糊查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtTodoTaskDTO", paramType = "query"),
        @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryFuzzyListForPage", method = RequestMethod.GET)
    public Result queryFuzzyListForPage(@ModelAttribute PtTodoTaskDTO paramsDTO,
                                                            @RequestParam(name = "appId", required = true) String appId,
                                                            @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页模糊查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        paramsDTO.setAppid(appId);
        List<PtTodoTaskDTO> resultDTOList = ptTodoTaskService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtTodoTaskDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页模糊查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页模糊查询成功", pageInfo);
    }

    /**
     * @title: queryListForPage
     * @description: 列表分页精确查询
     * @params: [ paramsDTO, currentPage, pageSize ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "列表分页查询", notes = "列表分页查询")
    @ApiImplicitParams({
    @ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtTodoTaskDTO", paramType = "query"),
    @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
    @ApiImplicitParam(name = "pageSize", value = "一页数据条数", required = true, dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryListForPage", method = RequestMethod.GET)
    public Result queryListForPage(@ModelAttribute PtTodoTaskDTO paramsDTO,
                                                       @RequestParam(name = "currentPage", required = true) Integer currentPage,
                                                       @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        log.info("准备执行  列表分页查询，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //分页
        Page page = PageHelper.startPage(currentPage, pageSize);
        //执行
        List<PtTodoTaskDTO> resultDTOList = ptTodoTaskService.selectFuzzyByDto(paramsDTO);
        PageInfo<PtTodoTaskDTO> pageInfo = page.toPageInfo();
        pageInfo.setList(resultDTOList);
        log.info("执行列表分页查询完成，准备返回的结果为：" + JSON.toJSONString(pageInfo));
        return new Result(true, 20000, "列表分页查询成功", pageInfo);
    }


    /**
     * @title: save
     * @description: 数据插入
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据插入", notes = "数据插入")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtTodoTask", paramType = "body")})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody PtTodoTaskDTO paramsDTO) {
        log.info("准备执行 数据插入，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptTodoTaskService.insertDto(paramsDTO);
        log.info("执行 数据插入完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据插入成功", result);
    }

    /**
     * @title: update
     * @description: 数据修改
     * @params: [ paramsDTO ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据修改", notes = "数据修改")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtTodoTask", paramType = "body")})
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody PtTodoTaskDTO paramsDTO) {
        log.info("准备执行 数据修改，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptTodoTaskService.updateDto(paramsDTO);
        log.info("执行 数据修改完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据修改成功", result);
    }

    /**
     * @title: delete
     * @description: 数据删除
     * @params: [ id ]
     * @return: org.springframework.http.ResponseEntity
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @ApiOperation(value = "数据删除", notes = "数据删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "paramsDTO", value = "实体参数信息", required = true, dataType = "PtTodoTask", paramType = "body")})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody PtTodoTaskDTO paramsDTO) {
        log.info("准备执行 数据删除，传入的参数为：" + JSON.toJSONString(paramsDTO));
        //执行
        Integer result = ptTodoTaskService.deleteDto(paramsDTO);
        log.info("执行 数据删除 完成，准备返回的结果为：" + JSON.toJSONString(result));
        return new Result(true, 20000, "数据删除", result);
    }

    @PostMapping("/initOrUpdateTodoTask")
    public Result<Boolean> initOrUpdateTodoTask(@RequestBody PtTodoTaskDTO ptTodoTaskDTO) {
        Boolean is = ptTodoTaskService.initOrUpdateTodoTask(ptTodoTaskDTO);
        return new Result<>(true, 20000, "成功", is);
    }
    
    /**
     * 待办
     * @return
     */
    @RequestMapping(value = "/sendTodoTask", method = RequestMethod.POST)
    public Result sendTodoTask(@RequestBody List<PtTodoTask> ptTodoTaskList){
        try {
            asyncSendMessage.addOrUpdateTodoTask(ptTodoTaskList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("公共能力发送待办失败:{}",e.getMessage());
        }
        return new Result(true, StatusCodeEnum.NORMAL);
    }

    /**
     * 待办
     * @return
     *
     * 在自己系统配置密码，通过参数把密码传过来
     * */
    @RequestMapping(value = "/sendTodoTaskWithPw", method = RequestMethod.POST)
    public Result sendTodoTaskWithPw(@RequestBody List<PtTodoTaskDTO> ptTodoTaskDTOList){
        try {
            asyncSendMessage.addOrUpdateTodoTaskWithPw(ptTodoTaskDTOList);
        }catch (Exception e){
            e.printStackTrace();
            log.error("公共能力发送待办失败:{}",e.getMessage());
        }
        return new Result(true, StatusCodeEnum.NORMAL);
    }


    /**
     * 待办查询
     * @return
     */
    @RequestMapping(value = "/queryTodoTask", method = RequestMethod.POST)
    public PtTodoTask queryTodoTask(@RequestBody PtTodoTask ptTodoTask){
        PtTodoTask ptTodoTask1 = new PtTodoTask();
        try {
            ptTodoTask1 = ptTodoTaskService.getTodoTaskTwo(ptTodoTask);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询待办失败:{}",e.getMessage());
        }
        return ptTodoTask1;
    }


}
